﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Teal.Compiler {

    #region 节点

    /// <summary>
    /// 表示一个语法树节点。
    /// </summary>
    public abstract partial class Node {

        ///// <summary>
        ///// 获取当前节点的起始位置。
        ///// </summary>
        //public abstract Location startLocation {
        //    get;
        //    set;
        //}

        ///// <summary>
        ///// 获取当前节点的结束位置。
        ///// </summary>
        //public abstract Location endLocation {
        //    get;
        //    set;
        //}

        ///// <summary>
        ///// 将当前节点的内容写入指定的输出器。
        ///// </summary>
        ///// <param name="writer">写入的目标对象。</param>
        //public abstract void write(IndentedTextWriter writer);

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public virtual Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public virtual Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public virtual void write(IndentedTextWriter writer) {

        }
        /// <summary>
        /// 返回表示当前对象的字符串。
        /// </summary>
        /// <returns>表示当前对象的字符串。</returns>
        public override string ToString() {
            StringBuilder sb = new StringBuilder();
            using (IndentedTextWriter writer = new IndentedTextWriter(new StringWriter(sb))) {
                write(writer);
            }
            return sb.ToString();
        }

    }

    /// <summary>
    /// 表示一个变量。
    /// </summary>
    public partial class Variable : Node {

        /// <summary>
        /// 支持多个对象组成一个单链表。
        /// </summary>
        public Variable next;

        /// <summary>
        /// 获取当前变量的变量类型。
        /// </summary>
        public VariableType variableType;

        /// <summary>
        /// 获取当前变量的类型。
        /// </summary>
        public Expression type;

        /// <summary>
        /// 获取当前变量的名字。
        /// </summary>
        public Identifier name;

        /// <summary>
        /// 获取当前变量的初始值。
        /// </summary>
        public Expression initialiser;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return name.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            type.write(writer);
            writer.Write(' ');
            name.write(writer);
            if (initialiser != null) {
                writer.Write(" = ");
                initialiser.write(writer);
            }
        }

    }

    /// <summary>
    /// 对变量类型的枚举。
    /// </summary>
    public enum VariableType {

        /// <summary>
        /// 表示这是一个普通变量。
        /// </summary>
        normalLocal,

        /// <summary>
        /// 表示这是一个常量。
        /// </summary>
        constLocal,

        /// <summary>
        /// 表示这是一个静态变量。
        /// </summary>
        staticLocal,

        /// <summary>
        /// 表示这是一个静态最终变量。
        /// </summary>
        finalLocal,

        /// <summary>
        /// 表示这是一个寄存变量。
        /// </summary>
        externLocal,

        /// <summary>
        /// 表示这是一个易变变量。
        /// </summary>
        volatileLocal,

        /// <summary>
        /// 表示这是一个外部变量。
        /// </summary>
        outLocal,

        /// <summary>
        /// 表示这是一个引用变量。
        /// </summary>
        refLocal,

        /// <summary>
        /// 标记为变量。
        /// </summary>
        PARAMETER,

        /// <summary>
        /// 表示这是一个输入参数。
        /// </summary>
        inParameter,

        /// <summary>
        /// 表示这是一个输出参数。
        /// </summary>
        outParameter,

        /// <summary>
        /// 表示这是一个引用参数。
        /// </summary>
        refParameter,

        /// <summary>
        /// 表示这是一个可变参数。
        /// </summary>
        paramsParameter,

        /// <summary>
        /// 表示这是一个参数列表。
        /// </summary>
        argListParameter,

    }

    #endregion

    #region 表达式

    #region Expression

    /// <summary>
    /// 表示一个表达式。
    /// </summary>
    public abstract partial class Expression : Node {

        /// <summary>
        /// 表示一个空表达式。主要用于错误表达式的占位符。
        /// </summary>
        private partial class EmptyExpression : Expression {

            /// <summary>
            /// 获取当前节点的起始位置。
            /// </summary>
            public override Location startLocation {
                get {
                    return new Location();
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 获取当前节点的结束位置。
            /// </summary>
            public override Location endLocation {
                get {
                    return new Location();
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                writer.Write("/*错误的表达式*/");
            }
        }

        /// <summary>
        /// 表示一个空表达式。主要用于表示错误表达式。
        /// </summary>
        public static readonly Expression empty = new EmptyExpression();

    }

    #endregion

    #region Identfiers

    /// <summary>
    /// 表示一个标识符。
    /// </summary>
    public partial class Identifier : Expression {

        /// <summary>
        /// 获取当前标识符的内容。
        /// </summary>
        public string value;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(value);
        }

    }

    /// <summary>
    /// 表示一个魔法变量。
    /// </summary>
    public partial class MagicVariable : Identifier {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write('@');
            base.write(writer);
        }

    }

    #endregion

    #region Literals

    /// <summary>
    /// 表示内置类型字面量。
    /// </summary>
    public partial class PredefinedTypeLiteral : Expression {

        /// <summary>
        /// 获取常量的类型。可能的值为 var void int ...
        /// </summary>
        public TokenType type;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        ///// <summary>
        ///// 获取当前节点的结束位置。
        ///// </summary>
        //public override Location endLocation {
        //    get {
        //        return startLocation + type.getName().Length;
        //    }
        //    set {
        //        throw new Unreachable();
        //    }
        //}

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(type.getName());
        }

    }

    /// <summary>
    /// 表示 null 常量。
    /// </summary>
    public partial class NullLiteral
        // : Expression
    {

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 4;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("null");
        }

    }

    /// <summary>
    /// 表示 true 常量。
    /// </summary>
    public partial class TrueLiteral : Expression {

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 4;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("true");
        }

    }

    /// <summary>
    /// 表示 false 常量。
    /// </summary>
    public partial class FalseLiteral : Expression {

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 5;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("false");
        }

    }

    /// <summary>
    /// 表示一个 32 位整数常量。
    /// </summary>
    public partial class IntLiteral
        // : Expression
    {

        /// <summary>
        /// 获取实际的值。
        /// </summary>
        public int value;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(value);
        }

    }

    /// <summary>
    /// 表示一个 64 位整数常量。
    /// </summary>
    public partial class LongLiteral
        // : Expression
    {

        /// <summary>
        /// 获取实际的值。
        /// </summary>
        public long value;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(value);
        }

    }

    /// <summary>
    /// 表示一个浮点数常量。
    /// </summary>
    public partial class FloatLiteral
        // : Expression
    {

        /// <summary>
        /// 获取实际的值。
        /// </summary>
        public double value;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标 <see cref="IndentedTextWriter" /> 对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(value);
        }

    }

    /// <summary>
    /// 表示一个字符串常量。
    /// </summary>
    public partial class StringLiteral
        // : Expression
    {

        /// <summary>
        /// 获取实际的值。
        /// </summary>
        public string value;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(Utility.escapeString(value));
        }

    }

    /// <summary>
    /// 表示一个列表字面量。
    /// </summary>
    public partial class ListLiteral : Expression {

        /// <summary>
        /// 获取表达式的类型。
        /// </summary>
        public TokenType type;

        /// <summary>
        /// 获取当前表达式的值列表。
        /// </summary>
        public List<Expression> values;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            if (type == TokenType.lBrack) {
                writer.Write('[');
            } else {
                writer.Write('{');
            }
            if (values != null) {
                for (int i = 0; i < values.Count; i++) {
                    if (i > 0) {
                        writer.Write(", ");
                    }
                    values[i].write(writer);
                }
            }
            if (type == TokenType.lBrack) {
                writer.Write(']');
            } else {
                writer.Write('}');
            }
        }

    }

    /// <summary>
    /// 表示一个字典字面量。
    /// </summary>
    public partial class DictLiteral : Expression {

        /// <summary>
        /// 获取表达式的类型。
        /// </summary>
        public TokenType type;

        /// <summary>
        /// 表示一个字典字面量中的一个键值对。
        /// </summary>
        public partial class Property {

            /// <summary>
            /// 支持多个对象组成单链表。
            /// </summary>
            public Property next;

            /// <summary>
            /// 获取属性的键。
            /// </summary>
            public Expression key;

            /// <summary>
            /// 获取属性的值。
            /// </summary>
            public Expression value;

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public void write(IndentedTextWriter writer) {
                key.write(writer);
                writer.Write(": ");
                value.write(writer);
            }

        }

        /// <summary>
        /// 获取当前表达式的值列表。
        /// </summary>
        public Property properties;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            if (type == TokenType.lBrack) {
                writer.Write('[');
            } else {
                writer.Write('{');
            }
            if (properties != null) {
                for (var property = properties; property != null; property = property.next) {
                    writer.Indent++;
                    property.write(writer);
                    if (property.next != null) {
                        writer.Write(", ");
                    }
                    writer.Indent--;
                    writer.WriteLine();
                }
            } else {
                writer.Write(':');
            }
            if (type == TokenType.lBrack) {
                writer.Write(']');
            } else {
                writer.Write('}');
            }
        }

    }

    /// <summary>
    /// 表示一个区间字面量。
    /// </summary>
    public partial class RangeLiteral : Expression {

        /// <summary>
        /// 获取区间的左值。
        /// </summary>
        public Expression start;

        /// <summary>
        /// 获取区间的右值。
        /// </summary>
        public Expression end;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            if (start != null) {
                start.write(writer);
                writer.Write(' ');
            }
            writer.Write('~');
            if (end != null) {
                writer.Write(' ');
                end.write(writer);
            }
        }

    }

    /// <summary>
    /// 表示 this 常量。
    /// </summary>
    public partial class ThisLiteral 
        // : Expression
    {

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 4;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("this");
        }

    }

    /// <summary>
    /// 表示 this 常量。
    /// </summary>
    public partial class BaseLiteral
        // : Expression
    {

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 4;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("base");
        }

    }

    /// <summary>
    /// 表示一个函数表达式。
    /// </summary>
    public partial class LambdaLiteral : Expression {

        /// <summary>
        /// 获取当前函数定义的参数列表。
        /// </summary>
        public Teal.Compiler.MemberDefinition.Parameter parameters;

        /// <summary>
        /// 获取当前函数的主体表达式部分。
        /// </summary>
        public Expression returnBody;

        /// <summary>
        /// 获取当前函数的主体部分。
        /// </summary>
        public ToplevelBlock body;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {

            //writer.Write('(');
            //for (Variable parameter = parameters; parameter != null; parameter = parameter.next) {
            //    parameter.write(writer);
            //    if (parameter.next != null) {
            //        writer.Write(", ");
            //    }
            //}
            //writer.Write(") -> ");

            //if (body != null)
            //    body.write(writer);
            //else
            //    returnBody.write(writer);
        }

    }

    //#endregion

    //#region ShimExpressions

    ///// <summary>
    ///// 表示一个括号表达式。
    ///// </summary>
    //public partial class ParenthesizedExpression : Expression {

    //    /// <summary>
    //    /// 获取当前表达式的主体。
    //    /// </summary>
    //    public Expression body;

    //    /// <summary>
    //    /// 获取当前节点的起始位置。
    //    /// </summary>
    //    public override Location startLocation {
    //        get;
    //        set;
    //    }

    //    /// <summary>
    //    /// 获取当前节点的结束位置。
    //    /// </summary>
    //    public override Location endLocation {
    //        get;
    //        set;
    //    }

    //    /// <summary>
    //    /// 将当前节点的内容写入指定的输出器。
    //    /// </summary>
    //    /// <param name="writer">写入的目标对象。</param>
    //    public override void write(IndentedTextWriter writer) {
    //        writer.Write('(');
    //        body.write(writer);
    //        writer.Write(')');
    //    }

    //}

    /// <summary>
    /// 表示一组逗号隔开的表达式。
    /// </summary>
    public partial class CommaExpression : Expression {

        /// <summary>
        /// 获取左边部分的表达式。
        /// </summary>
        public Expression left;

        /// <summary>
        /// 获取右边部分的表达式。
        /// </summary>
        public Expression right;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        /// <exception cref="System.AssertException"></exception>
        public override Location startLocation {
            get {
                return left.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        /// <exception cref="System.AssertException"></exception>
        public override Location endLocation {
            get {
                return right.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            left.write(writer);
            writer.Write(", ");
            right.write(writer);
        }

    }

    /// <summary>
    /// 表示一个三元条件表达式。
    /// </summary>
    public partial class ConditionalExpression : Expression {

        /// <summary>
        /// 获取当前表达式的条件。
        /// </summary>
        public Expression condition;

        /// <summary>
        /// 获取当前表达式的则部分。
        /// </summary>
        public Expression thenExpression;

        /// <summary>
        /// 获取当前表达式的否则部分。
        /// </summary>
        public Expression elseExpression;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return condition.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return elseExpression.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            condition.write(writer);
            writer.Write(" ? ");
            thenExpression.write(writer);
            writer.Write(" : ");
            elseExpression.write(writer);
        }

    }

    ///// <summary>
    ///// 表示一个类型转换表达式。
    ///// </summary>
    //public partial class CastExpression : Expression {

    //    /// <summary>
    //    /// 获取当前表达式的主体。
    //    /// </summary>
    //    public Expression targetType;

    //    /// <summary>
    //    /// 获取当前表达式的主体。
    //    /// </summary>
    //    public Expression body;

    //    /// <summary>
    //    /// 获取当前节点的起始位置。
    //    /// </summary>
    //    public override Location startLocation {
    //        get;
    //        set;
    //    }

    //    /// <summary>
    //    /// 获取当前节点的结束位置。
    //    /// </summary>
    //    public override Location endLocation {
    //        get {
    //            return body.endLocation;
    //        }
    //        set {
    //            throw new Unreachable();
    //        }
    //    }

    //    /// <summary>
    //    /// 将当前节点的内容写入指定的输出器。
    //    /// </summary>
    //    /// <param name="writer">写入的目标对象。</param>
    //    public override void write(IndentedTextWriter writer) {
    //        writer.Write('(');
    //        targetType.write(writer);
    //        writer.Write(')');
    //        body.write(writer);
    //    }

    //}

    #endregion

    #region TypeExpressions

    /// <summary>
    /// 表示一个泛型表达式。
    /// </summary>
    /// <remarks>
    /// 泛型表达式是由一个 Identifier 和泛型参数组成的表达式，在很多地方它可以代替 Identifier 作为一个整体。
    /// 如表达式 A.B&lt;int&gt; 最后解析结果是一个 PropertyCallExpression(target: Identifier("A"), argument: GenericExpression(baseType: Identifier("B"), genericArguments:[Identifier("int")])) 。
    /// </remarks>
    public partial class GenericTypeExpression : Expression {

        /// <summary>
        /// 获取当前泛型表达式的基础部分。
        /// </summary>
        public Identifier elementType;

        /// <summary>
        /// 获取当前泛型表达式的泛型参数。
        /// </summary>
        public List<Expression> genericArguments;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return elementType.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            elementType.write(writer);
            writer.Write('<');
            bool appendComma = false;
            foreach (Expression genericArgument in genericArguments) {
                if (appendComma) {
                    writer.Write(", ");
                } else {
                    appendComma = true;
                }
                if (genericArgument != null) {
                    genericArgument.write(writer);
                }
            }
            writer.Write('>');
        }

    }

    /// <summary>
    /// 表示一个数组类型表达式。
    /// </summary>
    public partial class ArrayTypeExpression : Expression {

        /// <summary>
        /// 获取当前类型的基础类型。
        /// </summary>
        public Expression elementType;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return elementType.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            elementType.write(writer);
            writer.Write('[');
            writer.Write(']');
        }

    }

    /// <summary>
    /// 表示一个指针表达式。
    /// </summary>
    public partial class PtrTypeExpression : Expression {

        /// <summary>
        /// 获取当前类型的基础类型。
        /// </summary>
        public Expression elementType;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return elementType.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            elementType.write(writer);
            writer.Write('*');
        }

    }

    #endregion

    #region CallExpressions

    /// <summary>
    /// 表示一个成员调用表达式。
    /// </summary>
    public partial class MemberCallExpression
        //  : Expression 
    {

        /// <summary>
        /// 获取当前调用的目标表达式。
        /// </summary>
        public Expression target;

        /// <summary>
        /// 获取当前调用的参数表达式。
        /// </summary>
        public Expression argument;

    //    /// <summary>
    //    /// 获取当前节点的起始位置。
    //    /// </summary>
    //    public override Location startLocation {
    //        get {
    //            return target.startLocation;
    //        }
    //        set {
    //            throw new Unreachable();
    //        }
    //    }

    //    /// <summary>
    //    /// 获取当前节点的结束位置。
    //    /// </summary>
    //    public override Location endLocation {
    //        get {
    //            return argument.endLocation;
    //        }
    //        set {
    //            throw new Unreachable();
    //        }
    //    }

    //    /// <summary>
    //    /// 将当前节点的内容写入指定的输出器。
    //    /// </summary>
    //    /// <param name="writer">写入的目标对象。</param>
    //    public override void write(IndentedTextWriter writer) {
    //        target.write(writer);
    //        writer.Write('.');
    //        argument.write(writer);
    //    }

    }

    /// <summary>
    /// 表示一个函数调用表达式。
    /// </summary>
    public partial class FuncCallExpression : Expression {

        /// <summary>
        /// 获取当前调用的目标表达式。
        /// </summary>
        public Expression target;

        /// <summary>
        /// 表示函数调用的参数。
        /// </summary>
        public partial class Argument : Node {

            /// <summary>
            /// 支持多个对象组成一个单链表。
            /// </summary>
            public Argument next;

            /// <summary>
            /// 获取参数名。如果当前参数不是命名参数，则返回 null 。
            /// </summary>
            public Identifier name;

            /// <summary>
            /// 判断当前参数的类型。
            /// </summary>
            public ArgumentType type;

            /// <summary>
            /// 获取调用的参数值。
            /// </summary>
            public Expression value;

            /// <summary>
            /// 获取当前节点的起始位置。
            /// </summary>
            public override Location startLocation {
                get {
                    return value.startLocation;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 获取当前节点的结束位置。
            /// </summary>
            public override Location endLocation {
                get {
                    return value.endLocation;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                if (name != null) {
                    writer.Write(name);
                    writer.Write(": ");
                }
                switch (type) {
                    case ArgumentType.@out:
                        writer.Write("out ");
                        break;
                    case ArgumentType.@ref:
                        writer.Write("ref ");
                        break;
                }
                value.write(writer);
            }

        }

        /// <summary>
        /// 表示函数实参的类型。
        /// </summary>
        public enum ArgumentType {

            /// <summary>
            /// 输入参数。
            /// </summary>
            @in,

            /// <summary>
            /// 输出参数。
            /// </summary>
            @out,

            /// <summary>
            /// 引用参数。
            /// </summary>
            @ref,

            /// <summary>
            /// 输出参数。
            /// </summary>
            @outAssignTo,

        }

        /// <summary>
        /// 获取当前调用的参数列表。
        /// </summary>
        public Argument arguments;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return target.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            target.write(writer);
            writer.Write('(');
            writeArguments(writer);
            writer.Write(')');
        }

        /// <summary>
        /// 写入参数。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        protected void writeArguments(IndentedTextWriter writer) {
            for (Argument argument = arguments; argument != null; argument = argument.next) {
                argument.write(writer);
                if (argument.next != null) {
                    writer.Write(", ");
                }
            }
        }

    }

    /// <summary>
    /// 表示一个 new 表达式。
    /// </summary>
    public partial class NewExpression 
        // : FuncCallExpression 
    {

        /// <summary>
        /// 获取当前初始化符号。
        /// </summary>
        public TokenType type;

        /// <summary>
        /// 获取当前表达式的初始化项。
        /// </summary>
        public Expression initializer;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点写入指定的 IndentedTextWriter 。
        /// </summary>
        /// <param name="writer">写入工具。</param>
        public override void write(IndentedTextWriter writer) {
            //writer.Write("new ");
            //target.write(writer);

            //if (type == TokenType.lParam) {
            //    writer.Write('(');
            //    writeArguments(writer);
            //    writer.Write(')');
            //} else {
            //    writer.Write('[');
            //    writeArguments(writer);
            //    writer.Write(']');
            //}

            //if (initializer != null) {
            //    initializer.write(writer);
            //}

        }

    }

    /// <summary>
    /// 表示一个索引调用表达式。
    /// </summary>
    public partial class IndexCallExpression : FuncCallExpression {

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            target.write(writer);
            writer.Write('[');
            writeArguments(writer);
            writer.Write(']');
        }

    }

    /// <summary>
    /// 表示一个链式成员访问表达式。
    /// </summary>
    public partial class ChainCallExpression : MemberCallExpression {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            target.write(writer);
            writer.Write("..");
            argument.write(writer);
        }

    }

    /// <summary>
    /// 表示一个链式表达式。
    /// </summary>
    public partial class ChainExpression : ParenthesizedExpression {

        /// <summary>
        /// 获取当前关联的链式表达式。
        /// </summary>
        public ChainCallExpression chainCallExpression;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            //  body.write(writer);
        }

    }

    #endregion

    #region ArithmeticExpressions

    /// <summary>
    /// 表示一个一元运算表达式。
    /// </summary>
    public partial class UnaryExpression : Expression {

        /// <summary>
        /// 获取当前表达式的运算符。可能的合法值是：+ - ! ^~ &amp; typeof await 。
        /// </summary>
        public TokenType @operator;

        /// <summary>
        /// 获取当前表达式的运算数。
        /// </summary>
        public Expression operand;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return operand.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(@operator.getName());
            if (@operator.isKeyword()) {
                writer.Write(' ');
            }
            operand.write(writer);
        }

    }

    /// <summary>
    /// 表示一个 typeof 或 sizeof 表达式。
    /// </summary>
    public abstract partial class TypeOrSizeOfExpression : Expression {

        /// <summary>
        /// 获取当前表达式的运算数。
        /// </summary>
        public Expression operand;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return operand.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

    }

    /// <summary>
    /// 表示一个 typeof 表达式。
    /// </summary>
    public partial class TypeOfExpression : TypeOrSizeOfExpression {

        /// <summary>
        /// 将当前节点写入指定的 <see cref="IndentedTextWriter" /> 对象。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("typeof(");
            operand.write(writer);
            writer.Write(")");
        }

    }

    /// <summary>
    /// 表示一个 sizeof 表达式。
    /// </summary>
    public partial class SizeOfExpression : TypeOrSizeOfExpression {

        /// <summary>
        /// 将当前节点写入指定的 <see cref="IndentedTextWriter" /> 对象。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("sizeof(");
            operand.write(writer);
            writer.Write(")");
        }

    }

    /// <summary>
    /// 表示一个 ++ 或 -- 运算表达式。
    /// </summary>
    public partial class MutatorExpression : UnaryExpression   {

        /// <summary>
        /// 判断当前表达式是否是前缀表达式。
        /// </summary>
        public bool prefix;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                if (prefix) {
                    return base.startLocation;
                }
                return operand.startLocation;
            }
            set {
                if (!prefix) {
                    throw new Unreachable();
                }
                base.startLocation = value;
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                if (prefix) {
                    return operand.endLocation;
                }
                return base.startLocation;
            }
            set {
                if (prefix) {
                    throw new Unreachable();
                }
                base.startLocation = value;
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            if (prefix) {
                operand.write(writer);
                writer.Write(@operator.getName());
            } else {
                writer.Write(@operator.getName());
                operand.write(writer);
            }
        }

    }

    /// <summary>
    /// 表示一个二元运算表达式。
    /// </summary>
    public partial class BinaryExpression : Expression {

        /// <summary>
        /// 获取当前表达式的左值。
        /// </summary>
        public Expression leftOperand;

        /// <summary>
        /// 获取当前表达式的运算符。合法的值为 + - * / ^ % &amp;&amp; || %&gt; %&lt; %&amp; %| %^ is as or。
        /// </summary>
        public TokenType @operator;

        /// <summary>
        /// 获取当前表达式的右值。
        /// </summary>
        public Expression rightOperand;

        /// <summary>
        /// 获取或设置节点结束位置。
        /// </summary>
        public override Location startLocation {
            get {
                return leftOperand.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取或设置节点结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return rightOperand.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            leftOperand.write(writer);
            writer.Write(' ');
            writer.Write(@operator.getName());
            writer.Write(' ');
            rightOperand.write(writer);
        }

    }

    /// <summary>
    /// 表示一个 is 或 as 表达式。
    /// </summary>
    public abstract partial class IsOrAsExpression : Expression {

        /// <summary>
        /// 获取当前表达式的左值。
        /// </summary>
        public Expression leftOperand;

        /// <summary>
        /// 获取当前表达式的右值。
        /// </summary>
        public Expression rightOperand;

        /// <summary>
        /// 获取或设置节点结束位置。
        /// </summary>
        public override Location startLocation {
            get {
                return leftOperand.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取或设置节点结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return rightOperand.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

    }

    /// <summary>
    /// 表示一个 is 表达式。
    /// </summary>
    public partial class IsExpression : IsOrAsExpression {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            leftOperand.write(writer);
            writer.Write(" is ");
            rightOperand.write(writer);
        }

    }

    /// <summary>
    /// 表示一个 as 表达式。
    /// </summary>
    public partial class AsExpression : IsOrAsExpression {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            leftOperand.write(writer);
            writer.Write(" as ");
            rightOperand.write(writer);
        }

    }

    #endregion

    #endregion

    #region 语句

    #region Statement

    /// <summary>
    /// 表示一个语句。
    /// </summary>
    public abstract partial class Statement : Node {

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

    }

    /// <summary>
    /// 表示一个空语句。
    /// </summary>
    public partial class Semicolon : Statement {

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        /// </exception>
        public override Location endLocation {
            get {
                return startLocation + 1;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write(";");
        }


        protected override bool DoFlowAnalysis(FlowAnalysisContext fc) {
            throw new NotImplementedException();
        }

        protected override void DoEmit(EmitContext ec) {
            throw new NotImplementedException();
        }

        protected override void CloneTo(CloneContext clonectx, Statement target) {
            throw new NotImplementedException();
        }
    }

    #endregion

    #region Blocks

    ///// <summary>
    ///// 表示一个语句块。
    ///// </summary>
    //public partial class Block : Statement {

    //    /// <summary>
    //    /// 获取当前块内的所有语句。
    //    /// </summary>
    //    public List<Statement> statements;

    //    /// <summary>
    //    /// 获取当前节点的结束位置。
    //    /// </summary>
    //    public override Location endLocation {
    //        get;
    //        set;
    //    }

    //    /// <summary>
    //    /// 将当前节点的内容写入指定的输出器。
    //    /// </summary>
    //    /// <param name="writer">写入的目标对象。</param>
    //    public override void write(IndentedTextWriter writer) {
    //        writer.WriteLine('{');
    //        writer.Indent++;

    //        if (statements != null) {
    //            foreach (var statement in statements) {
    //                statement.write(writer);
    //                writer.WriteLine();
    //            }
    //        }

    //        writer.Indent--;
    //        writer.Write('}');
    //    }

    //}

    ///// <summary>
    ///// 表示一个最顶层的语句块。
    ///// </summary>
    //public partial class ToplevelBlock : Block {

    //}

    #endregion

    #region ShimStatements

    /// <summary>
    /// 表示一个变量声明语句。
    /// </summary>
    public partial class VariableStatement : Statement {

        /// <summary>
        /// 获取当前变量的类型。
        /// </summary>
        public Expression type {
            get {
                return variables.type;
            }
        }

        /// <summary>
        /// 获取当前语句的所有变量。
        /// </summary>
        public Variable variables;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location endLocation {
            get {
                return variables.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {

            switch (variables.variableType) {
                case VariableType.normalLocal:
                    break;
                case VariableType.constLocal:
                    writer.Write("const ");
                    break;
            }

            if (type != null) {
                type.write(writer);
                writer.Write(' ');
            }

            for (Variable variable = variables; variable != null; variable = variable.next) {
                variable.name.write(writer);
                if (variable.initialiser != null) {
                    writer.Write(" = ");
                    variable.initialiser.write(writer);
                }

                if (variable.next != null) {
                    writer.Write(", ");
                }
            }

            writer.Write(';');

        }


        protected override bool DoFlowAnalysis(FlowAnalysisContext fc) {
            throw new NotImplementedException();
        }

        protected override void DoEmit(EmitContext ec) {
            throw new NotImplementedException();
        }

        protected override void CloneTo(CloneContext clonectx, Statement target) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个标签语句。
    /// </summary>
    public partial class LabeledStatement : Statement {

        /// <summary>
        /// 获取当前的标签。
        /// </summary>
        public Identifier label;

        /// <summary>
        /// 获取当前的主要语句。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return label.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return label.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Indent--;
            label.write(writer);
            writer.Write(':');
            writer.WriteLine();
            writer.Indent++;
            if (body != null) {
                body.write(writer);
            }
        }

    }

    /// <summary>
    /// 表示一个表达式语句。
    /// </summary>
    public partial class ExpressionStatement
        //: Statement 
    {

        /// <summary>
        /// 获取当前表达式语句的主体。
        /// </summary>
        public Expression body;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return body.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            body.write(writer);
            writer.Write(';');
        }

    }

    #endregion

    #region SelectionStatements

    /// <summary>
    /// 表示一个 if 语句。
    /// </summary>
    public partial class IfStatement : Statement {

        /// <summary>
        /// 获取条件部分。
        /// </summary>
        public Expression condition;

        /// <summary>
        /// 获取当前的则的部分。
        /// </summary>
        public Statement thenClause;

        /// <summary>
        /// 获取当前的否则的部分。
        /// </summary>
        public Statement elseClause;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 2;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("if (");
            condition.write(writer);
            writer.Write(")");
            writer.Indent++;
            thenClause.write(writer);
            writer.Indent--;

            if (elseClause != null) {
                writer.Write(" else ");
                writer.Indent++;
                elseClause.write(writer);
                writer.Indent--;
            }

        }

    }

    /// <summary>
    /// 表示一个 switch 语句。
    /// </summary>
    public partial class SwitchStatement
        //: Statement 
    {

        /// <summary>
        /// 获取判断的条件。
        /// </summary>
        public Expression condition;

        /// <summary>
        /// 表示一个 case 块。
        /// </summary>
        public partial class CaseClause : Node {

            /// <summary>
            /// 支持多个对象组成一个单链表。
            /// </summary>
            public CaseClause next;

            /// <summary>
            /// 获取当前 case 的标签。如果标签是 null，表示是 else 分支。
            /// </summary>
            public Expression label;

            /// <summary>
            /// 获取当前的全部语句。如果 body 是 null，表示当前 case 是一个直接贯穿的 case 。
            /// </summary>
            public Block body;

            /// <summary>
            /// 获取当前节点的起始位置。
            /// </summary>
            public override Location startLocation {
                get;
                set;
            }

            /// <summary>
            /// 获取当前节点的结束位置。
            /// </summary>
            public override Location endLocation {
                get;
                set;
            }

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                writer.Write("case ");
                if (label == null) {
                    writer.Write("else");
                } else {
                    label.write(writer);
                }
                writer.WriteLine(": ");
                writer.Indent++;
                if (body != null) {
                    foreach (Statement st in body.statements) {
                        st.write(writer);
                    }
                }
                writer.Indent--;
            }

        }

        /// <summary>
        /// 获取全部选项。
        /// </summary>
        public CaseClause caseClauses;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 6;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("switch ");
            if (condition != null) {
                writer.Write('(');
                condition.write(writer);
                writer.Write(") ");
            }
            writer.WriteLine("{");
            writer.Indent++;

            bool lastIsEmpty = false;
            for (var caseClause = caseClauses; caseClause != null; caseClause = caseClause.next) {
                if (lastIsEmpty) {
                    writer.Write(", ");
                } else {
                    writer.Write("case ");
                }
                if (caseClause.label == null) {
                    writer.Write("else");
                } else {
                    caseClause.label.write(writer);
                }
                if (caseClause.body == null) {
                    lastIsEmpty = true;
                } else {
                    lastIsEmpty = false;
                    writer.WriteLine(':');
                    caseClause.body.write(writer);
                    writer.WriteLine();
                }
            }

            writer.Indent--;
            writer.WriteLine("}");

        }

    }

    #endregion

    #region IterationStatements

    /// <summary>
    /// 表示一个 for 语句。
    /// </summary>
    public partial class ForStatement
        //: Statement 
    {

        /// <summary>
        /// 获取初始化语句。
        /// </summary>
        public Node initializer;

        /// <summary>
        /// 获取条件部分。
        /// </summary>
        public Expression condition;

        /// <summary>
        /// 获取下一次语句。
        /// </summary>
        public Expression iterator;

        /// <summary>
        /// 获取主体部分。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 3;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {

            writer.Write("for (");

            if (initializer != null) {
                initializer.write(writer);
            }
            writer.Write(';');
            if (condition != null) {
                condition.write(writer);
            }
            writer.Write(';');
            if (iterator != null) {
                iterator.write(writer);
            }

            writer.Write(')');
            body.write(writer);
        }

    }

    /// <summary>
    /// 表示一个 for in 语句。
    /// </summary>
    public partial class ForInStatement
        //: Statement 
    {

        /// <summary>
        /// 获取全部变量。
        /// </summary>
        public Variable variable;

        /// <summary>
        /// 获取目标表达式。
        /// </summary>
        public Expression iterator;

        /// <summary>
        /// 获取主体部分。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 3;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("for (");
            variable.write(writer);
            writer.Write(" in ");
            iterator.write(writer);
            writer.Write(")");
            body.write(writer);
        }

    }

    /// <summary>
    /// 表示一个 for to 语句。
    /// </summary>
    public partial class ForToStatement : Statement {

        /// <summary>
        /// 获取循环的目标表达式。
        /// </summary>
        public Variable variable;

        /// <summary>
        /// 获取结束表达式。
        /// </summary>
        public Expression end;

        /// <summary>
        /// 获取下一次语句。
        /// </summary>
        public Expression iterator;

        /// <summary>
        /// 获取主体部分。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 3;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {

            writer.Write("for (");
            variable.write(writer);
            writer.Write(" to ");
            end.write(writer);

            if (iterator != null) {
                writer.Write(';');
                iterator.write(writer);
            }

            writer.Write(')');
            body.write(writer);
        }


        protected override bool DoFlowAnalysis(FlowAnalysisContext fc) {
            throw new NotImplementedException();
        }

        protected override void DoEmit(EmitContext ec) {
            throw new NotImplementedException();
        }

        protected override void CloneTo(CloneContext clonectx, Statement target) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个 while 语句。
    /// </summary>
    public partial class WhileStatement
        //: Statement 
    {

        /// <summary>
        /// 获取条件部分。
        /// </summary>
        public Expression condition;

        /// <summary>
        /// 获取主体部分。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 5;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("while (");
            condition.write(writer);
            writer.Write(')');
            body.write(writer);
        }

    }

    /// <summary>
    /// 表示一个 do while 语句。
    /// </summary>
    public partial class DoWhileStatement
        //: Statement 
    {

        /// <summary>
        /// 获取条件部分。
        /// </summary>
        public Expression condition;

        /// <summary>
        /// 获取主体部分。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 2;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("do");
            body.write(writer);
            writer.Write("while");
            condition.write(writer);
            writer.Write(')');
            writer.Write(';');
        }

    }

    #endregion

    #region JumpStatements

    /// <summary>
    /// 表示一个 continue 语句。
    /// </summary>
    public partial class ContinueStatement
        //: Statement 
    {

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.WriteLine("continue;");
        }

    }

    /// <summary>
    /// 表示一个 break 语句。
    /// </summary>
    public partial class BreakStatement
        //: Statement 
    {

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.WriteLine("break;");
        }


    }

    /// <summary>
    /// 表示一个跳转到标签的跳到语句。
    /// </summary>
    public partial class GotoLabelStatement : Statement {

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取目标标签。
        /// </summary>
        public Identifier target;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("goto ");
            target.write(writer);
            writer.Write(';');
        }


        protected override bool DoFlowAnalysis(FlowAnalysisContext fc) {
            throw new NotImplementedException();
        }

        protected override void DoEmit(EmitContext ec) {
            throw new NotImplementedException();
        }

        protected override void CloneTo(CloneContext clonectx, Statement target) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个跳转到 case 语句的跳到语句。
    /// </summary>
    public partial class GotoCaseStatement
        //: Statement 
    {

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取目标标签。
        /// </summary>
        public Expression target;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("goto case ");
            if (target == null) {
                writer.Write("else");
            } else {
                target.write(writer);
            }
            writer.Write(';');
        }

    }

    /// <summary>
    /// 表示一个 return 语句。
    /// </summary>
    public partial class ReturnStatement
        //: Statement 
    {

        /// <summary>
        /// 获取返回的表达式。
        /// </summary>
        public Expression value;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("return");
            if (value != null) {
                writer.Write(' ');
                value.write(writer);
            }

            writer.Write(';');
        }

    }

    /// <summary>
    /// 表示一个 yield 语句。
    /// </summary>
    public partial class YieldStatement : Statement {

        /// <summary>
        /// 获取返回的表达式。
        /// </summary>
        public Expression value;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.WriteLine("yield ");
            value.write(writer);
            writer.Write(';');
        }


        protected override bool DoFlowAnalysis(FlowAnalysisContext fc) {
            throw new NotImplementedException();
        }

        protected override void DoEmit(EmitContext ec) {
            throw new NotImplementedException();
        }

        protected override void CloneTo(CloneContext clonectx, Statement target) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个 throw 语句。
    /// </summary>
    public partial class ThrowStatement : Statement {

        /// <summary>
        /// 获取表达式。
        /// </summary>
        public Expression value;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get;
            set;
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("throw");
            if (value != null) {
                writer.Write(' ');
                value.write(writer);
            }
            writer.Write(';');
        }

    }

    #endregion

    #region ExceptionStatements

    /// <summary>
    /// 表示一个 try 语句。
    /// </summary>
    public partial class TryStatement : Statement {

        /// <summary>
        /// 获取 try 语句块。
        /// </summary>
        public Statement tryClause;

        /// <summary>
        /// 表示一个 catch 块。
        /// </summary>
        public partial class CatchClause : Node {

            /// <summary>
            /// 支持多个对象组成一个单链表。
            /// </summary>
            public CatchClause next;

            /// <summary>
            /// 获取当前捕获的异常类型。
            /// </summary>
            public Variable variable;

            /// <summary>
            /// 当前块的主体。
            /// </summary>
            public Statement body;

            /// <summary>
            /// 获取当前节点的起始位置。
            /// </summary>
            public override Location startLocation {
                get;
                set;
            }

            /// <summary>
            /// 获取当前节点的结束位置。
            /// </summary>
            public override Location endLocation {
                get {
                    return startLocation + 5;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                writer.Write("catch");
                if (variable != null) {
                    writer.Write('(');
                    variable.type.write(writer);
                    if (variable.name != null) {
                        writer.Write(' ');
                        variable.name.write(writer);
                    }
                    writer.Write(')');
                }
                body.write(writer);
            }

        }

        /// <summary>
        /// 获取当前的所有 catch 块。
        /// </summary>
        public CatchClause catchClauses;

        /// <summary>
        /// 获取 finally 语句块。
        /// </summary>
        public Statement finallyClause;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 3;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("try ");
            tryClause.write(writer);
            for (var catchClause = catchClauses; catchClause != null; catchClause = catchClause.next) {
                catchClause.write(writer);
            }
            if (finallyClause != null) {
                writer.Write("finally");
                finallyClause.write(writer);
            }
        }


        protected override bool DoFlowAnalysis(FlowAnalysisContext fc) {
            throw new NotImplementedException();
        }

        protected override void DoEmit(EmitContext ec) {
            throw new NotImplementedException();
        }

        protected override void CloneTo(CloneContext clonectx, Statement target) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个 with 语句。
    /// </summary>
    public partial class WithStatement 
        //: Statement 
    {

        /// <summary>
        /// 获取当前的目标表达式。
        /// </summary>
        public Node target;

        /// <summary>
        /// 获取主体部分。
        /// </summary>
        public Statement body;

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 4;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("with (");
            if (target is Variable) {
                Variable variable = (Variable)target;
                variable.type.write(writer);
                writer.Write(' ');
                for (; variable != null; variable = variable.next) {
                    variable.write(writer);
                    if (variable.next != null) {
                        writer.Write(", ");
                    }
                }
            } else {
                target.write(writer);
            }
            writer.Write(")");
            body.write(writer);
        }

    }

    #endregion

    #endregion

    #region 成员定义

    #region MemberDefinition

    /// <summary>
    /// 表示一个成员（如方法、字段、类、模块等）定义。
    /// </summary>
    public abstract partial class MemberDefinition : Node {

        /// <summary>
        /// 支持多个对象组成一个单链表。
        /// </summary>
        public MemberDefinition next;

        /// <summary>
        /// 获取当前成员的文档注释。如果不存在文档注释则返回 null。
        /// </summary>
        public DocComment docComment;

        /// <summary>
        /// 表示一个注解。
        /// </summary>
        public partial class MemberAnnotation : FuncCallExpression {

            /// <summary>
            /// 支持多个对象组成一个单链表。
            /// </summary>
            public MemberAnnotation next;

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                writer.Write('@');
                base.write(writer);
            }

        }

        /// <summary>
        /// 获取当前成员的所有注解。
        /// </summary>
        public MemberAnnotation annotations;

        /// <summary>
        /// 获取当前成员的修饰符。
        /// </summary>
        public Modifiers modifiers;

        static readonly Identifier _emptyIdentifier = new Identifier() {
            value = String.Empty
        };

        /// <summary>
        /// 获取当前成员的名字。
        /// </summary>
        public Identifier name = _emptyIdentifier;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get {
                return name.startLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return name.endLocation;
            }
            set {
                throw new Unreachable();
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        protected void writeHeader(IndentedTextWriter writer) {

            for (var annotation = annotations; annotation != null; annotation = annotation.next) {
                annotation.write(writer);
                writer.Write(' ');
            }

            if (modifiers != Modifiers.none) {
                writer.Write(modifiers.getNames());
                writer.Write(' ');
            }

        }

        /// <summary>
        /// 表示一个函数参数。
        /// </summary>
        public partial class Parameter : Variable {

            /// <summary>
            /// 获取当前节点的起始位置。
            /// </summary>
            public override Location startLocation {
                get {
                    return name.startLocation;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 获取或设置节点在源文件中的位置。
            /// </summary>
            public override Location endLocation {
                get {
                    return name.endLocation;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {

                switch (variableType) {
                    case VariableType.inParameter:
                        break;
                    case VariableType.outParameter:
                        writer.Write("out ");
                        break;
                    case VariableType.refParameter:
                        writer.Write("ref ");
                        break;
                    case VariableType.paramsParameter:
                        writer.Write("params ");
                        break;
                    case VariableType.argListParameter:
                        writer.Write("...");
                        return;
                }

                if (type != null) {
                    type.write(writer);
                    writer.Write(' ');
                }

                name.write(writer);

                if (initialiser != null) {
                    writer.Write(' ');
                    writer.Write('=');
                    writer.Write(' ');
                    initialiser.write(writer);
                }

            }

        }

        /// <summary>
        /// 表示一个泛型参数。
        /// </summary>
        public partial class GenericParameter : Node {

            /// <summary>
            /// 支持多个对象组成一个单链表。
            /// </summary>
            public GenericParameter next;

            /// <summary>
            /// 获取当前参数的名字。
            /// </summary>
            public Identifier name;

            /// <summary>
            /// 表示一个泛型约束表达式。
            /// </summary>
            public abstract partial class ConstraintExpression : Expression {

                /// <summary>
                /// 获取当前节点的起始位置。
                /// </summary>
                public override Location startLocation {
                    get;
                    set;
                }

            }

            /// <summary>
            /// 表示结构约束表达式。
            /// </summary>
            public sealed partial class StructConstraintExpression : ConstraintExpression {

                /// <summary>
                /// 获取当前节点的结束位置。
                /// </summary>
                public override Location endLocation {
                    get {
                        return startLocation + 6;
                    }
                    set {
                        throw new Unreachable();
                    }
                }

                /// <summary>
                /// 将当前节点的内容写入指定的输出器。
                /// </summary>
                /// <param name="writer">写入的目标对象。</param>
                public override void write(IndentedTextWriter writer) {
                    writer.Write("struct");
                }

            }

            /// <summary>
            /// 表示类约束表达式。
            /// </summary>
            public sealed partial class ClassConstraintExpression : ConstraintExpression {

                /// <summary>
                /// 获取当前节点的结束位置。
                /// </summary>
                public override Location endLocation {
                    get {
                        return startLocation + 5;
                    }
                    set {
                        throw new Unreachable();
                    }
                }

                /// <summary>
                /// 将当前节点的内容写入指定的输出器。
                /// </summary>
                /// <param name="writer">写入的目标对象。</param>
                public override void write(IndentedTextWriter writer) {
                    writer.Write("class");
                }

            }

            /// <summary>
            /// 表示枚举约束表达式。
            /// </summary>
            public sealed partial class EnumConstraintExpression : ConstraintExpression {

                /// <summary>
                /// 获取当前节点的结束位置。
                /// </summary>
                public override Location endLocation {
                    get {
                        return startLocation + 4;
                    }
                    set {
                        throw new Unreachable();
                    }
                }

                /// <summary>
                /// 将当前节点的内容写入指定的输出器。
                /// </summary>
                /// <param name="writer">写入的目标对象。</param>
                public override void write(IndentedTextWriter writer) {
                    writer.Write("enum");
                }

            }

            /// <summary>
            /// 表示默认构造函数约束表达式。
            /// </summary>
            public sealed partial class NewableConstraintExpression : ConstraintExpression {

                /// <summary>
                /// 获取当前节点的结束位置。
                /// </summary>
                public override Location endLocation {
                    get;
                    set;
                }

                /// <summary>
                /// 将当前节点的内容写入指定的输出器。
                /// </summary>
                /// <param name="writer">写入的目标对象。</param>
                public override void write(IndentedTextWriter writer) {
                    writer.Write("new()");
                }

            }

            /// <summary>
            /// 获取当前泛型的约束。
            /// </summary>
            public List<Expression> constraints;

            /// <summary>
            /// 获取当前节点的起始位置。
            /// </summary>
            public override Location startLocation {
                get {
                    return name.startLocation;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 获取当前节点的结束位置。
            /// </summary>
            public override Location endLocation {
                get {
                    return name.endLocation;
                }
                set {
                    throw new Unreachable();
                }
            }

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                name.write(writer);
                if (constraints != null) {
                    writer.Write(" : ");
                    if (constraints.Count == 1) {
                        constraints[0].write(writer);
                    } else {
                        writer.Write("(");
                        bool appendComma = false;
                        foreach (var constraint in constraints) {
                            if (appendComma) {
                                writer.Write(", ");
                            } else {
                                appendComma = true;
                            }
                            constraint.write(writer);
                        }
                        writer.Write(")");
                    }
                }

            }

        }

    }

    #endregion

    #region TypeDefinitions

    /// <summary>
    /// 表示一个可以保存子成员的容器成员定义。
    /// </summary>
    public abstract partial class MemberContainerDefinition : MemberDefinition {

        /// <summary>
        /// 获取当前容器内的所有成员。
        /// </summary>
        public MemberDefinition members;

        /// <summary>
        /// 将当前子成员写入指定的 <see cref="IndentedTextWriter" /> 对象。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        protected void writeMembers(IndentedTextWriter writer) {
            writer.Write("{");
            writer.Indent++;
            for (MemberDefinition member = members; member != null; member = member.next) {
                writer.WriteLine();
                member.write(writer);
                writer.WriteLine();
            }
            writer.Indent--;
            writer.WriteLine("}");
        }

    }

    /// <summary>
    /// 表示一个类型（如类、结构、接口）定义。
    /// </summary>
    public abstract partial class TypeDefinition : MemberContainerDefinition {

        /// <summary>
        /// 获取当前类型的基类型列表。
        /// </summary>
        public List<Expression> baseTypes;

        /// <summary>
        /// 获取当前类型的泛型形参列表。非泛型则返回 null 。
        /// </summary>
        public GenericParameter genericParameters;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        /// <param name="memberTypeName">成员名。</param>
        protected void writeType(IndentedTextWriter writer, string memberTypeName) {
            writeHeader(writer);
            writer.Write(memberTypeName);
            writer.Write(' ');
            name.write(writer);
            if (genericParameters != null) {
                writer.Write('<');
                genericParameters.write(writer);
                writer.Write('>');
            }

            if (baseTypes != null) {
                bool appendComma = false;
                foreach (var baseType in baseTypes) {
                    if (appendComma) {
                        writer.Write(", ");
                    } else {
                        writer.Write(" : ");
                        appendComma = true;
                    }
                    baseType.write(writer);
                }
            }

            base.writeMembers(writer);
        }

    }

    /// <summary>
    /// 表示一个类定义。
    /// </summary>
    public partial class ClassDefinition : TypeDefinition {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            base.writeType(writer, "class");
        }

    }

    /// <summary>
    /// 表示一个结构定义。
    /// </summary>
    public partial class StructDefinition : TypeDefinition {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            base.writeType(writer, "struct");
        }

    }

    /// <summary>
    /// 表示一个接口定义。
    /// </summary>
    public partial class InterfaceDefinition : TypeDefinition {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            base.writeType(writer, "interface");
        }

    }

    /// <summary>
    /// 表示一个枚举定义。
    /// </summary>
    public partial class EnumDefinition : TypeDefinition {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            writer.Write("enum ");
            name.write(writer);

            //if (baseType != null) {
            //    writer.Write(" : ");
            //    baseType.write(writer);
            //}

            base.writeMembers(writer);
        }

    }

    /// <summary>
    /// 表示一个扩展定义。
    /// </summary>
    public partial class ExtensionDefinition : MemberContainerDefinition {

        /// <summary>
        /// 获取当前要扩展的目标类型表达式。
        /// </summary>
        public Expression targetType;

        /// <summary>
        /// 获取当前类型的基类型列表。
        /// </summary>
        public List<Expression> baseTypes;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            writer.Write("extend ");
            targetType.write(writer);

            if (baseTypes != null) {
                bool appendComma = false;
                foreach (var baseType in baseTypes) {
                    if (appendComma) {
                        writer.Write(", ");
                    } else {
                        writer.Write(" : ");
                        appendComma = true;
                    }
                    baseType.write(writer);
                }
            }

            base.writeMembers(writer);
        }


        public override void ApplyAttributeBuilder(MemberAnnotation a, MethodReference ctor, byte[] cdata, PredefinedAttributes pa) {
            throw new NotImplementedException();
        }
    }

    #endregion

    #region NamespaceDefinitions

    /// <summary>
    /// 表示一个命名空间定义。
    /// </summary>
    public partial class NamespaceDefinition : MemberContainerDefinition {

        /// <summary>
        /// 获取当前的命名空间。
        /// </summary>
        public List<Identifier> names;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            writer.Write("namespace ");
            if (names != null) {
                bool appendComma = false;
                foreach (Identifier name in names) {
                    if (appendComma) {
                        writer.Write('.');
                    } else {
                        appendComma = true;
                    }
                    name.write(writer);
                }
            } else {
                name.write(writer);
            }
            base.writeMembers(writer);
        }


        public override void ApplyAttributeBuilder(MemberDefinition.MemberAnnotation a, MethodReference ctor, byte[] cdata, PredefinedAttributes pa) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个独立的源编译单元。
    /// </summary>
    public partial class SourceUnit : MemberContainerDefinition {

        /// <summary>
        /// 获取当前单元内的导入列表。
        /// </summary>
        public ImportDirective importDirectives;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            for (ImportDirective import = importDirectives; import != null; import = import.next) {
                import.write(writer);
            }
            if (importDirectives != null) {
                writer.WriteLine();
                writer.WriteLine();
            }

            for (MemberDefinition member = members; member != null; member = member.next) {
                member.write(writer);
                writer.WriteLine();
                writer.WriteLine();
            }
        }

        public override void ApplyAttributeBuilder(MemberAnnotation a, MethodReference ctor, byte[] cdata, PredefinedAttributes pa) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 表示一个 import 指令。
    /// </summary>
    public partial class ImportDirective : Node {

        /// <summary>
        /// 支持多个对象组成一个单链表。
        /// </summary>
        public ImportDirective next;

        /// <summary>
        /// 获取当前指令的别名部分。
        /// </summary>
        public Identifier alias;

        /// <summary>
        /// 获取当前指令的值部分。
        /// </summary>
        public Expression value;

        /// <summary>
        /// 获取当前节点的起始位置。
        /// </summary>
        public override Location startLocation {
            get;
            set;
        }

        /// <summary>
        /// 获取当前节点的结束位置。
        /// </summary>
        public override Location endLocation {
            get {
                return startLocation + 6;
            }
            set {
                throw new Unreachable();
            }
        }


        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writer.Write("import ");
            if (alias != null) {
                alias.write(writer);
                writer.Write(" = ");
            }
            value.write(writer);

            writer.Write(";");
        }

    }

    /// <summary>
    /// 表示一个模块。
    /// </summary>
    public partial class ModuleDefinition : MemberContainerDefinition {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            for (MemberDefinition member = members; member != null; member = member.next) {
                member.write(writer);
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine();
            }
        }

    }

    #endregion

    #region TypeMemberDefinitions

    /// <summary>
    /// 表示一个类型子成员定义。
    /// </summary>
    public abstract partial class TypeMemberDefinition : MemberDefinition {

    }

    /// <summary>
    /// 表示一个字段定义。
    /// </summary>
    public partial class FieldDefinition : TypeMemberDefinition {

        /// <summary>
        /// 获取当前字段的所有变量。
        /// </summary>
        public Variable variables;

        /// <summary>
        /// 获取当前变量的类型。
        /// </summary>
        public Expression type {
            get {
                return variables.type;
            }
        }

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            type.write(writer);
            writer.Write(' ');

            for (Variable variable = variables; variable != null; variable = variable.next) {
                variable.name.write(writer);
                if (variable.initialiser != null) {
                    writer.Write(" = ");
                    variable.initialiser.write(writer);
                }
                if (variable.next != null) {
                    writer.Write(", ");
                }
            }

            writer.Write(';');
        }

    }

    /// <summary>
    /// 表示一个方法或属性定义。
    /// </summary>
    public abstract partial class MethodOrPropertyDefinition : TypeMemberDefinition {

        /// <summary>
        /// 获取当前成员的返回类型。
        /// </summary>
        public Expression returnType;

        /// <summary>
        /// 获取当前成员被显式声明的所有者。
        /// </summary>
        public Expression explicitType;

    }

    /// <summary>
    /// 表示一个属性或索引器定义。
    /// </summary>
    public abstract partial class PropertyOrIndexerDefinition : MethodOrPropertyDefinition {

        /// <summary>
        /// 表示一个属性访问器。
        /// </summary>
        public partial class PropertyAccessor : TypeMemberDefinition {

            /// <summary>
            /// 获取当前定义对应的成员类型。
            /// </summary>
            protected override MemberType memberType {
                get {
                    return MemberType.method;
                }
            }

            /// <summary>
            /// 获取访问器的主体。（可能为 null）
            /// </summary>
            public ToplevelBlock body;

            /// <summary>
            /// 将当前节点的内容写入指定的输出器。
            /// </summary>
            /// <param name="writer">写入的目标对象。</param>
            public override void write(IndentedTextWriter writer) {
                switch (modifiers) {
                    case Modifiers.none:
                        break;
                    case Modifiers.@protected:
                        writer.Write("protected ");
                        break;
                    case Modifiers.@private:
                        writer.Write("private ");
                        break;
                    case Modifiers.@public:
                        writer.Write("public ");
                        break;

                }
                writer.Write(name);
                writer.Write(' ');
                if (body != null) {
                    body.write(writer);
                } else {
                    writer.Write(';');
                }
            }


            public override bool Define() {
                throw new NotImplementedException();
            }

            public override void ApplyAttributeBuilder(MemberAnnotation a, MethodReference ctor, byte[] cdata, PredefinedAttributes pa) {
                throw new NotImplementedException();
            }

        }

        /// <summary>
        /// 获取 get 访问器部分。
        /// </summary>
        public PropertyAccessor getAccessor;

        /// <summary>
        /// 获取 set 访问器部分。
        /// </summary>
        public PropertyAccessor setAccessor;

    }

    /// <summary>
    /// 表示一个属性定义。
    /// </summary>
    public partial class PropertyDefinition
        // : MemberDefinition 
    {

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            returnType.write(writer);
            writer.Write(' ');
            if (explicitType != null) {
                explicitType.write(writer);
                writer.Write(".");
            }

            writer.Write(name);
            writer.Write(' ');
            writer.WriteLine('{');
            writer.Indent++;

            if (getAccessor != null) {
                getAccessor.write(writer);
                writer.WriteLine();
            }

            if (setAccessor != null) {
                setAccessor.write(writer);
                writer.WriteLine();
            }

            writer.Indent--;
            writer.WriteLine('}');
        }

    }

    /// <summary>
    /// 表示一个索引器定义。
    /// </summary>
    public partial class IndexerDefinition : PropertyOrIndexerDefinition
    {

        /// <summary>
        /// 获取当前定义的参数列表。
        /// </summary>
        public Parameter parameters;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            //writeHeader(writer);
            //returnType.write(writer);
            //writer.Write(' ');
            //writer.WriteLine('[');

            //for (Variable parameter = parameters; parameter != null; parameter = parameter.next) {
            //    parameter.write(writer);
            //    if (parameter.next != null) {
            //        writer.Write(", ");
            //    }
            //}

            //writer.WriteLine("] {");
            //writer.Indent++;

            //if (getAccessor != null) {
            //    getAccessor.write(writer);
            //    writer.WriteLine();
            //}

            //if (setAccessor != null) {
            //    setAccessor.write(writer);
            //    writer.WriteLine();
            //}

            //writer.Indent--;
            //writer.WriteLine('}');

        }

    }

    /// <summary>
    /// 表示一个方法或构造函数定义。
    /// </summary>
    public abstract partial class MethodOrConstructorDefinition : MethodOrPropertyDefinition, IMethodData, IMethodDefinition {

        /// <summary>
        /// 获取当前函数定义的参数列表。
        /// </summary>
        public Parameter parameters;

        /// <summary>
        /// 获取当前函数定义的主体。
        /// </summary>
        public ToplevelBlock body;

        /// <summary>
        /// 写入参数到指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        protected void writeParameters(IndentedTextWriter writer) {
            writer.Write('(');
            for (Variable parameter = parameters; parameter != null; parameter = parameter.next) {
                parameter.write(writer);
                if (parameter.next != null) {
                    writer.Write(", ");
                }
            }
            writer.Write(')');
        }

    }

    /// <summary>
    /// 表示一个方法定义。
    /// </summary>
    public partial class MethodDefinition: MethodOrConstructorDefinition  {

        /// <summary>
        /// 获取成员的泛型参数。
        /// </summary>
        public GenericParameter genericParameters;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            if (returnType == null) {
                writer.Write("func ");
            } else {
                returnType.write(writer);
                writer.Write(' ');
            }
            if (explicitType != null) {
                explicitType.write(writer);
                writer.Write(".");
            }

            name.write(writer);

            if (genericParameters != null) {
                writer.Write('<');
                genericParameters.write(writer);
                writer.Write('>');
            }

            writeParameters(writer);

            if (body != null)
                body.write(writer);
            else
                writer.Write(';');

        }

    }

    /// <summary>
    /// 表示一个构造函数定义。
    /// </summary>
    public partial class ConstructorDefinition : MethodOrConstructorDefinition {

        ///// <summary>
        ///// 获取初始化部分。
        ///// </summary>
        //public TokenType initializerType;

        ///// <summary>
        ///// 获取所有初始化参数。
        ///// </summary>
        //public FuncCallExpression.Argument initializerArguments;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);
            name.write(writer);
            writeParameters(writer);
            body.write(writer);
        }

    }

    /// <summary>
    /// 表示操作符重载定义。
    /// </summary>
    public partial class OperatorDefinition 
       // : MethodOrConstructorDefinition
    {

        /// <summary>
        /// 获取重载的操作符。可能为 ++、--、+、-、*、/、%、^、&lt;、&gt;、&lt;=、&gt;=、==、!=、as、new、%&gt;、%&lt;、%^、%~ 、%&amp;、%| 。
        /// </summary>
        public TokenType @operator;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);

            returnType.write(writer);
            writer.Write(" this ");
            writer.Write(@operator.getName());

            writeParameters(writer);

            if (body != null)
                body.write(writer);
            else
                writer.Write(';');

        }

    }

    /// <summary>
    /// 表示一个枚举的成员定义。
    /// </summary>
    public partial class EnumMemberDefinition : TypeMemberDefinition {

        /// <summary>
        /// 获取当前枚举成员的初始化表达式（可能为 null）。
        /// </summary>
        public Expression initializer;

        /// <summary>
        /// 将当前节点的内容写入指定的输出器。
        /// </summary>
        /// <param name="writer">写入的目标对象。</param>
        public override void write(IndentedTextWriter writer) {
            writeHeader(writer);

            if (initializer != null) {
                writer.Write(" = ");
                initializer.write(writer);
            }

            writer.Write(',');
        }

        public override bool Define() {
            throw new NotImplementedException();
        }

        public override void ApplyAttributeBuilder(MemberAnnotation a, MethodReference ctor, byte[] cdata, PredefinedAttributes pa) {
            throw new NotImplementedException();
        }
    }

    #endregion

    #endregion

}